package com.yb.guigu.pick.core.bean;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.yb.guigu.pick.core.constant.GuiguStatus;
import com.yb.guigu.pick.core.exception.GuiguBusinessException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/7 14:50
 * FileName: XczxResult
 * Description:
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel
public class GuiguResult<T> {

    @ApiModelProperty("响应码")
    private Integer code;
    @ApiModelProperty("响应文字")
    private String msg;

    @ApiModelProperty("响应数据")
    private T data;

    public static GuiguResult success(){
        return new GuiguResult(GuiguStatus.HttpStatus.SUCCESS.getCode(), GuiguStatus.HttpStatus.SUCCESS.getMsg(),null);
    }

    public static GuiguResult success(String msg){
        return new GuiguResult(GuiguStatus.HttpStatus.SUCCESS.getCode(), msg,null);
    }

    public static <R> GuiguResult success(R data){
        return new GuiguResult(GuiguStatus.HttpStatus.SUCCESS.getCode(), GuiguStatus.HttpStatus.SUCCESS.getMsg(),data);
    }

    public static GuiguResult error(){
        return new GuiguResult(GuiguStatus.HttpStatus.ERROR.getCode(), GuiguStatus.HttpStatus.ERROR.getMsg(),null);
    }
    public static <R> GuiguResult error(String msg){
        return new GuiguResult(GuiguStatus.HttpStatus.ERROR.getCode(), msg,null);
    }

    public static <R> GuiguResult error(String msg,R data){
        return new GuiguResult(GuiguStatus.HttpStatus.ERROR.getCode(), msg,data);
    }
    public static <R> GuiguResult error(R data){
        return new GuiguResult(GuiguStatus.HttpStatus.ERROR.getCode(), GuiguStatus.HttpStatus.ERROR.getMsg(),data);
    }


    public static GuiguResult error(Integer code, String msg){
        return new GuiguResult(code,msg,null);
    }

    public static  <R> GuiguResult error(Integer code, String msg , R data){
        return new GuiguResult(code,msg,data);
    }

    public static  <R> GuiguResult error(GuiguStatus.HttpStatus status){
        return new GuiguResult(status.getCode(), status.getMsg(), null);
    }

    public static  <R> GuiguResult error(GuiguStatus.HttpStatus status, R data){
        return new GuiguResult(status.getCode(), status.getMsg(), data);
    }

    public static  <R> GuiguResult error(GuiguStatus.BusinessStatus status){
        return new GuiguResult(status.getCode(), status.getMsg(), null);
    }

    public static  <R> GuiguResult error(GuiguStatus.BusinessStatus status, R data){
        return new GuiguResult(status.getCode(), status.getMsg(), data);
    }

    public static GuiguResult clientError(){
        return new GuiguResult(GuiguStatus.CLIENT_ERROR_CODE, GuiguStatus.HttpStatus.ERROR.getMsg(),null);
    }

    public static <R> GuiguResult clientError(R data){
        return new GuiguResult(GuiguStatus.CLIENT_ERROR_CODE, GuiguStatus.HttpStatus.ERROR.getMsg(),data);
    }

    public static GuiguResult clientError(Integer code, String msg){
        return new GuiguResult(code,msg,null);
    }
    public static  <R> GuiguResult clientError(Integer code, String msg , R data){
        return new GuiguResult(code,msg,data);
    }

    public static  <R> GuiguResult clientError(GuiguStatus.HttpStatus status){
        return new GuiguResult(status.getCode(), status.getMsg(), null);
    }

    public static  <R> GuiguResult clientError(GuiguStatus.HttpStatus status, R data){
        return new GuiguResult(status.getCode(), status.getMsg(), data);
    }
    public static GuiguResult error(GuiguBusinessException e){
        return new GuiguResult(e.getCode(),e.getMessage(),null);
    }

    @JSONField(serialize = false)
    public String toJSON(){
        return JSONObject.toJSONString(this);
    }



    public GuiguResult data(T data){
        this.data=data;
        return this;
    }

    public GuiguResult msg(String msg){
        this.msg=msg;
        return this;
    }

    public GuiguResult code(Integer code){
        this.code=code;
        return this;
    }
}
